<template>
  <div class="book-search-container">
    <!-- 分类导航 -->
    <el-row type="flex" justify="center" class="category-nav">
      <el-radio-group v-model="activeCategory" @change="handleCategoryChange">
        <el-radio-button
          v-for="category in categoryList"
          :key="category.id"
          :label="category.id"
          >{{ category.name }}</el-radio-button
        >
      </el-radio-group>
    </el-row>

    <!-- 搜索框 -->
    <el-row class="search-box">
      <el-col :span="12" :offset="6">
        <el-input
          placeholder="输入书名/作者"
          v-model="searchQuery"
          clearable
          @clear="handleSearchClear"
          @keyup.enter.native="handleSearch"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="handleSearch"
          />
        </el-input>
      </el-col>
    </el-row>

    <!-- 图书列表 -->
    <el-row :gutter="20" class="book-list">
      <el-col
        style="margin-top: 15px"
        v-for="book in bookList"
        :key="book.isbn"
        :xs="12"
        :sm="8"
        :md="6"
        :lg="4"
      >
        <el-card
          :body-style="{ padding: '0px', height: '100%' }"
          shadow="hover"
          @click.native="showDetail(book.isbn)"
        >
          <img
            :src="getSrc(book.coverUrl)"
            class="book-cover"
            :alt="book.bookName"
            @error="handleImageError"
          />
          <div class="book-info">
            <h4 class="book-title">{{ book.bookName }}</h4>
            <el-tooltip
              :content="book.author"
              placement="top"
              :disabled="book.author.length <= 10"
            >
              <p class="book-author">
                {{
                  book.author.length > 10
                    ? book.author.substring(0, 10)
                    : book.author
                }}
              </p>
            </el-tooltip>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 分页 -->
    <el-row type="flex" justify="center" class="pagination">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="pagination.pageSizes"
        :page-size="pagination.pageSize"
        :layout="
          isMobile ? 'prev, pager, next' : 'total, prev, pager, next, sizes'
        "
        :small="isMobile"
        :total="pagination.total"
      />
    </el-row>
  </div>
</template>
  
  <script>
export default {
  name: "HomePage",
  data() {
    return {
      defaultCover: require("@/assets/nopic.png"),
      activeCategory: 0,
      categoryList: [{ id: 0, name: "全部" }],
      searchQuery: "",
      bookList: [],
      pagination: {
        currentPage: 1,
        pageSizes: [12],
        pageSize: 12,
        total: 0,
      },
      isMobile: false,
      screenWidth: 0,
    };
  },
  created() {
    this.fetchCategory();
    this.fetchBooks();
  },
  mounted() {
    // 初始化时获取屏幕宽度
    this.screenWidth = window.innerWidth;
    this.checkCollapse();

    // 监听窗口大小变化
    window.addEventListener("resize", this.handleResize);
  },
  beforeDestroy() {
    // 组件销毁时移除监听
    window.removeEventListener("resize", this.handleResize);
  },
  methods: {
    handleResize() {
      this.screenWidth = window.innerWidth;
      this.checkCollapse();
    },
    checkCollapse() {
      // 当屏幕宽度小于 768px 时折叠菜单
      this.isMobile = this.screenWidth < 768;
    },
    handleImageError(e) {
      e.target.src = this.defaultCover;
    },
    async fetchCategory() {
      this.categoryList = [{ id: 0, name: "全部" }];
      const params = {
        search: "",
        curPage: 1,
        pageSize: 100,
      };
      const response = await this.$request.get("/api/category", {
        params: params,
      });
      if (response.code == 200) {
        this.categoryList = [...this.categoryList, ...response.data.records];
      }
    },
    getSrc(coverUrl) {
      if (coverUrl) {
        return coverUrl;
      } else {
        return this.defaultCover;
      }
    },
    showDetail(isbn) {
      this.$router.push({
        path: "/bookDetail/" + isbn,
      });
    },
    async fetchBooks() {
      const params = {
        category: this.activeCategory,
        search: this.searchQuery,
        curPage: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
      };
      const response = await this.$request.get("/api/book", {
        params: params,
      });
      if (response.code == 200) {
        this.bookList = response.data.records;
        this.pagination.total = response.data.total;
      }
    },
    handleCategoryChange() {
      this.pagination.currentPage = 1;
      this.fetchBooks();
    },
    handleSearch() {
      this.pagination.currentPage = 1;
      this.fetchBooks();
    },
    handleSearchClear() {
      this.searchQuery = "";
      this.fetchBooks();
    },
    handleCurrentChange(page) {
      this.pagination.currentPage = page;
      this.fetchBooks();
    },
  },
};
</script>
  
  <style scoped>
.book-search-container {
  padding: 0 20px 0 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.category-nav {
  margin-bottom: 20px;
}

.search-box {
  margin-bottom: 15px;
}

.book-list {
  margin-bottom: 15px;
}

.book-cover {
  width: 100%;
  height: 200px;
  object-fit: scale-down;
  border-bottom: 1px solid #ebeef5;
}

.book-info {
  padding: 14px;
}

.book-title {
  margin: 0 0 8px 0;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-author {
  margin: 0;
  font-size: 12px;
  color: #909399;
}

.pagination {
  margin-top: 30px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .search-box .el-col {
    width: 100%;
    margin-left: 0 !important;
  }
}
</style>
  